// 导入 express 模块
const express = require('express')

// 创建express服务器
const app = express()

// 配置解析application/x-www-form-urlencoded表单数据的中间件   一定要放在配置路由的前边
app.use(express.urlencoded({ extended: false }))

//  封装res.cc函数
// 响应数据的中间件
app.use((req, res, next) => {
    // status = 0 为成功； status = 1 为失败； 默认将 status 的值设置为 1，方便处理失败的情况
    res.cc = function (err, status = 1) {
        res.send({
            // 状态
            status,
            // 状态描述 判断err 是错误对象 还是 字符串
            message: err instanceof Error ? err.message : err
        })
    }
    next()
})

// 配置中间件
const cors = require('cors')
app.use(cors())


// 导入并使用商品分类路由模块
const goodsListRouter = require('./router/goodsList')
app.use('/goods', goodsListRouter)


// 分类的详情
const goodsCateRouter = require('./router/goodsCate')
app.use('/goods', goodsCateRouter)


// 茶书分类
const goodsBooksListRouter = require('./router/goodsCate')
app.use('/goods', goodsBooksListRouter)

// 茶学分类
const goodsXueListRouter = require('./router/goodsCate')
app.use('/goods', goodsXueListRouter)

// 茶服分类
const goodsChafuListRouter = require('./router/goodsCate')
app.use('/goods', goodsChafuListRouter)


// 商品详情页
const goodsDetailRouter = require('./router/goodsDetail')
app.use('/goods', goodsDetailRouter)

//商品加入购物车
const goodsCartRouter = require('./router/goodsCart')
app.use('/goods', goodsCartRouter)

// 查询购物车
const goodsCartListRouter = require('./router/goodsCartList')
app.use('/goods', goodsCartListRouter)


// 删除购物车
const goodsDeleteCartRouter = require('./router/goods_delete')
app.use('/goods', goodsDeleteCartRouter)

// 添加结算列表
const orderRouter = require('./router/addorder')
app.use('/goods', orderRouter)

// 结算结算列表
const orderListRouter = require('./router/addorder')
app.use('/goods', orderListRouter)

// 订单列表
const submitOrderListRouter = require('./router/addorder')
app.use('/goods', submitOrderListRouter)

// 查询订单
const selectOrderRouter = require('./router/addorder')
app.use('/goods', selectOrderRouter)

// 发起支付
const payMentRouter = require('./router/addorder')
app.use('/goods', payMentRouter)

// 支付后的状态
const successPaymentRouter = require('./router/addorder')
app.use('/goods', successPaymentRouter)

//
const noPaymentRouter = require('./router/addorder')
app.use('/goods', noPaymentRouter)

const yiPaymentRouter = require('./router/addorder')
app.use('/goods', yiPaymentRouter)



// 启动服务器
app.listen(3008, function () {
    console.log('api serve running at http://127.0.0.1:3008');
})


